/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author xDrake
 */
public abstract class MainBean {

    protected HttpServletRequest request = null;
    protected HttpServletResponse response = null;
    protected HttpSession session = null;
    protected ServletContext context = null;
    protected String realPath = "";
    protected boolean error = false;
    protected String lastErrorsMessage = "";

    abstract public boolean isLoad();

    public boolean isError() {
        return error;
    }

    public String getLastErrorsMessage() {
        return lastErrorsMessage;
    }

    public HttpSession getSession() {
        return session;
    }

    public void setSession(HttpSession session) {
        this.session = session;
    }
    
    public void setSessionParam(String param, Object value) {
        this.session.setAttribute(param, value);
    }

    public Object getSessionParam(String param) {
        if (this.session.getAttribute(param) != null) {
            return this.session.getAttribute(param);
        }else{
            return null;
        }
    }
    
    public ServletContext getContext() {
        return context;
    }

    public void setContext(ServletContext context) {
        this.context = context;
        this.realPath = context.getRealPath("");             
    }

    public String getApplicationPath() {
        return realPath;
    }
    
    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    public long getLongFromParam(String param) {
        String value = request.getParameter(param);
        if (value == null) {
            return 0;
        }
        try {
            return Long.parseLong(value);
        } catch (Exception e) {
            return 0;
        }
    }

    public String getStringFromParam(String param) {
        String value = request.getParameter(param);
        if (value == null || value.isEmpty()) {
            return "";
        }
        return value;
    }
}